<?php
if (!isset($_SESSION['nip'], $_SESSION['username'])) {
    header("Location: index.php?page=login");
}
?>

<?php include '_header.php'; ?>
<?php include '_menupanel.php'; ?>
<?php include '_menubar.php'; ?>

<script type="text/javascript">
    var url;
    var isUpdate;
    var isOpened;
    var selectedKuliah;
    var selectedMatkul;
    var selectedDosen;
    var selectedTahun;

    function create() {
        isUpdate = false;
        $('#dialog-form').dialog('open').dialog('setTitle', 'Create Perkuliahan');
        $('#form').form('clear');
        $("#combo-matkul").focus();
    }
	
    function edit() {
        isUpdate = true;
        selectedMatkul = null;
		selectedDosen = null;
		selectedTahun = null;
        selectedKuliah = $('#datagrid').datagrid('getSelected');
		if (selectedKuliah) {
            $('#dialog-form').dialog('open').dialog('setTitle', 'Edit Perkuliahan');
            $('#form').form('load', selectedKuliah);
            isOpened = true;
        }
    }
	
    function closeform() {
        $('#dialog-form').dialog('close');
        isOpened = false;
    }

    function save() {
        var matkul = $("#combo-matkul").combobox("getValue");
        var dosen = $("#combo-dosen").combobox("getValue");
        var tahun = $("#combo-tahun").combobox("getValue");
        var form_data = $("#form").serialize();

        if (!matkul) {
            $("#combo-matkul").focus();
			$.messager.show({title: 'Message',
                             msg: 'Mata kuliah belum di pilih',
                             timeout: 3000, showType: 'slide'});
            return false();
        }
        if (!dosen) {
            $("#combo-dosen").focus();
			$.messager.show({title: 'Message',
                             msg: 'Dosen pengajar belum di pilih',
                             timeout: 3000, showType: 'slide'});
            return false();
        }
        if (!tahun) {
            $("#combo-tahun").focus();
			$.messager.show({title: 'Message',
                             msg: 'Tahun ajaran belum di pilih',
                             timeout: 3000, showType: 'slide'});
            return false();
        }

		var update_data = "";
		if(isUpdate){
			update_data = "id_det_matkul=" + selectedKuliah.id_det_matkul;
			update_data += selectedMatkul == null ? "" : "&id_matkul=" + selectedMatkul.id_matkul;
			update_data += selectedDosen == null ? "" : "&nip=" + selectedDosen.nip;
			update_data += selectedTahun == null ? "" : "&tahun_ajaran=" + selectedTahun.Tahun;
		}
		
        form_data = isUpdate ? update_data : form_data;
        url = isUpdate ? "controller/kuliah_crud.php?act=upd" : "controller/kuliah_crud.php?act=add";
		
		$.ajax({
            type: "POST",
            url: url,
            data: form_data,
            success: function(data) {
                $.messager.show({
                    title: 'Message',
                    msg: data,
                    timeout: 2000,
                    showType: 'slide'
                });
                $('#datagrid').datagrid('reload');
				$('#combo-matkul').combobox('clear');
				$('#combo-matkul').combobox('reload');
                if (!isUpdate) {
                    $("#combo-matkul").focus();
                    $('#form').form('clear');
                }
            }
        });
    }
    function hapus() {
        var checked = [];
        var rows = $('#datagrid').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].kode_matkul;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id_det_matkul;
        }

        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menghapus ' + item + '?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/kuliah_crud.php?act=del",
                        data: 'id_det_matkul=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $('#datagrid').datagrid('reload');
							$('#combo-matkul').combobox('clear');
							$('#combo-matkul').combobox('reload');
                        }
                    });
                }
            });
        }
    }

    function refresh() {
        $('#datagrid').datagrid('reload');
		$('#combo-matkul').combobox('reload');
    }
    function doSearch(value) {
        $('#datagrid').datagrid('load', {
            cari: value
        });
    }

    $(document).ready(function() {
        $('#datagrid').datagrid({
            onClickRow: function(rowIndex, rowData) {
                if (isOpened) {
                    edit(rowData);
                }
            }
        });
		
		$('#combo-matkul').combobox({
            onSelect: function(data) {
                selectedMatkul = data;
            }
        });
		
		$('#combo-dosen').combobox({
            onSelect: function(data) {
                selectedDosen = data;
            }
        });
		
		$('#combo-tahun').combobox({
            onSelect: function(data) {
                selectedTahun = data;
            }
        });
    });
</script>

<div class="pageContent">

    <div class="main">

        <div class="container">
            <div align="center">
                <h1>Data Master Perkuliahan WP-Learning</h1>
                <h2>Anda dapat melakukan manipulasi data master perkuliahan di halaman ini</h2>
            </div>
        </div>

        <div class="content">

            <div id="tb" style="padding:2px;height:30px;">
                <div style="float:left;">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="create()">Tambah</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit(null)">Edit</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="hapus()">Hapus</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refresh()">Refresh</a>
                </div>
                <div style="float: right; padding: 2px;">
                    <input id="cari" class="easyui-searchbox" data-options="prompt:' Cari..', searcher:doSearch" style="width:200px"></input> 
                </div>
            </div>

            <table id="datagrid" title="Master Perkuliahan" class="easyui-datagrid" url="controller/kuliah_crud.php?act=list" 
                   toolbar="#tb" pagination="true" rownumbers="true" fitColumns="true" collapsible="true"
                   checkOnSelect="false" selectOnCheck="false" singleSelect="true" >
                <thead>
                    <tr>
                        <th data-options="field:'ck',checkbox:true"></th>
                        <th data-options="field:'id_det_matkul'" hidden="true"></th>
                        <th data-options="field:'id_matkul'" hidden="true"></th>
                        <th data-options="field:'nip'" hidden="true"></th>
                        <th data-options="field:'kode_matkul',width:70" sortable="true">Kode Mata Kuliah</th>
                        <th data-options="field:'matkul',width:180" sortable="true">Nama Mata Kuliah</th>
                        <th data-options="field:'namadosen',width:180" sortable="true">Dosen Pengajar</th>
                        <th data-options="field:'sks',width:50,align:'right'" sortable="true">SKS</th>
                        <th data-options="field:'tahun_ajaran',width:50,align:'right'" sortable="true">Tahun Ajaran</th>
                    </tr>
                </thead>
            </table>

            <!-- Dialog Form -->
            <div id="dialog-form" closed="true" buttons="#dialog-buttons" resizable="true" class="easyui-dialog" style="width:380px; height:180px; padding: 10px 10px" >
                <div id="isUpdate" ></div>
                <form id="form" method="post" novalidate>
                    <table class="data-form">
                        <tr>
                            <td>Mata Kuliah</td>
                            <td>
                                <input type="text" style="width: 240px" class="easyui-combobox" required="true" name="matkul" id="combo-matkul"
                                       data-options="url:'controller/kuliah_crud.php?act=get_exkuliah', method:'get',
                                       valueField:'id_matkul', textField:'matkul'"/>
                            </td>
                        </tr>
                        <tr>
                            <td>Dosen Pengajar</td>
                            <td>
                                <input type="text" style="width: 240px" class="easyui-combobox" required="true" name="namadosen" id="combo-dosen"
                                       data-options="url:'controller/kuliah_crud.php?act=get_dosen', method:'get',
                                       valueField:'nip', textField:'nama'"/>
                            </td>
                        </tr>
                        <tr>
                            <td>Tahun Ajaran</td>
                            <td><input type="text" style="width: 100px" class="inputbox2 easyui-combobox" required="true" name="tahun_ajaran" id="combo-tahun" 
                                       data-options="url:'ws/angkatan_services.php?action=get', method:'get',
                                       valueField:'Tahun', textField:'Tahun'"/>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <!-- Dialog Button -->
            <div id="dialog-buttons">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">Simpan</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeform()">Tutup</a>
            </div>

        </div>

        <?php include '_footer.php'; ?>
    </div>
</div

</body>
</html>